﻿using System;
using System.Windows;
using System.Windows.Controls;

using Perseus;

using Hetymine.Executioner.Commands;

namespace Hetymine.Executioner.Plugins.Commands.FTP {
    /// <summary>
    /// Interaction logic for FTPCommandConfigurator.xaml
    /// </summary>
    public partial class FTPCommandConfigurator : UserControl, ICommandConfigurator<ICommand> {        
        private FTPCommand _Command;
        private bool _IsApply = false;

        public FTPCommandConfigurator() {
            InitializeComponent();

            this.cName.TextChanged += new TextChangedEventHandler(Name_TextChanged);
            this.cCallStrings.TextChanged += new TextChangedEventHandler(CallStrings_TextChanged);

            this.cHost.TextChanged += new TextChangedEventHandler(Host_TextChanged);
            this.cPort.TextChanged += new TextChangedEventHandler(Port_TextChanged);            
            this.cPassive.Click += new RoutedEventHandler(Passive_Click);
            this.cUsername.TextChanged += new TextChangedEventHandler(Username_TextChanged);
            this.cPassword.PasswordChanged += new RoutedEventHandler(Password_PasswordChanged);
            
            this.cFileDirectory.TextChanged += new TextChangedEventHandler(FileDirectory_TextChanged);
            this.cCopy.Click += new RoutedEventHandler(Copy_Click);
            this.cCopyUrl.TextChanged += new TextChangedEventHandler(CopyUrl_TextChanged);
        }

        private void Name_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Name = this.cName.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void CallStrings_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            string[] calls = this.cCallStrings.Text.Split(
                ",",
                SplitOptions.Trim | SplitOptions.RemoveEmptyEntries
            );

            this._Command.CallStrings.Clear();
            this._Command.CallStrings.AddRange(calls);

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }

        private void Host_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Host = this.cHost.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void Port_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Port = int.Parse(this.cPort.Text);

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }

        private void Passive_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Passive = this.cPassive.IsChecked.Value;

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void Username_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Username = this.cUsername.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void Password_PasswordChanged(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Password = this.cPassword.Password;

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }

        private void FileDirectory_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.FileDirectory = this.cFileDirectory.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void Copy_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.Copy = this.cCopy.IsChecked.Value;

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }
        private void CopyUrl_TextChanged(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Command.CopyUrl = this.cCopyUrl.Text.Trim();

            this.OnCommandNameChanged(this, EventArgs.Empty);
        }

        #region ICommandConfigurator<ICommand> Members
        public void InitializeConfig(ICommand config) {
            this._Command = config as FTPCommand;

            this._IsApply = false;

            // Information
            this.cName.Text = this._Command.Name;
            this.cCallStrings.Text = String.Join(", ", this._Command.CallStrings.ToArray());

            // Host
            this.cHost.Text = this._Command.Host;
            this.cPort.Text = this._Command.Port.ToString();
            this.cPassive.IsChecked = this._Command.Passive;
            this.cUsername.Text = this._Command.Username;
            this.cPassword.Password = this._Command.Password;

            // Advanced
            this.cFileDirectory.Text = this._Command.FileDirectory;
            this.cCopy.IsChecked = this._Command.Copy;
            this.cCopyUrl.Text = this._Command.CopyUrl;

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }

        public event EventHandler CommandNameChanged;
        protected virtual void OnCommandNameChanged(object sender, EventArgs e) {
            if (this.CommandNameChanged != null) {
                this.CommandNameChanged(sender, e);
            }
        }
        #endregion
    }
}
